package com.hu.hrm.service.impl;

import com.hu.hrm.domain.Employee;
import com.hu.hrm.domain.Tenant;
import com.hu.hrm.dto.EnteringDTO;
import com.hu.hrm.mapper.EmployeeMapper;
import com.hu.hrm.mapper.MealMapper;
import com.hu.hrm.mapper.TenantMapper;
import com.hu.hrm.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author woo
 * @since 2020-02-20
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private MealMapper mealMapper;

    @Override
    public void entering(EnteringDTO enteringDTO) {
        /*保存操作
        很多时候都需要准备一个对象来存数据
        * */
        //1.保存租户
        Date date = new Date();
        Tenant tenant = enteringDTO.getTenant();
        //对应数据库中内容，发现需要补充信息
        tenant.setRegisterTime(date);
        //tenant.setState(false);
        baseMapper.insert(tenant);

        //2.保存租户的 管理员 账号
        //获取传来的employee
        Employee employee = enteringDTO.getEmployee();
        employee.setInputTime(date);
        //员工状态 正常
        employee.setState(Employee.STATE_NORMAL);
        //机构id
        employee.setTenantId(tenant.getId());
        //用户类型：管理员
        employee.setType(Employee.TYPE_ADMIN);

        employeeMapper.insert(employee);

        //3.租户和套餐中间表  自定义实现添加  参数： 套餐ID  机构ID 状态
        mealMapper.insertRelationMealAndTenant(enteringDTO.getMealId() , tenant.getId(),0);

        System.out.println(enteringDTO);
    }
}
